// mypage top
function clsMypageTop()
{
	this._HTMLDom = null;
	this.memberInfo = null;
	
	this.bLoaded = false;
	this._loadpage = _ROOT + "/inc/mypage_top.html";
	this._load();
}

clsMypageTop.prototype = {
	_load : function(){
		var thisClass = this;
		
		if($("header>div#top_cont>div.top_mypage").length > 0){
			$.ajax({
				url: this._loadpage
				, type: "get"
				, dataType: "html"
				, async: false
				, success: function(ret, sta){
					$("header>div#top_cont>div.top_mypage").prepend(ret);
					
					$("nav a").attr("id", "idPermissionLink");

					thisClass._init();
				}
			});
			//$("header>div#top_cont>div.top_mypage").load(this._loadpage, function(){thisClass._init();});
		}
	}
	, _init: function(){
		var thisClass = this;
		
		var $layer = $("header>div#top_cont>div.top_mypage");
		
		this._HTMLDom = {
			"$layer": $layer
		}
		
		$layer.find("p.my_photo:first img").width(135).height(135);
		
		$layer.find("dl.my_profile li:eq(5)").contents().filter(function(){
			return (this.nodeType==3 && this.data.trim()=="비거리");
		}).after("평균타수 ").remove();
		
		this.setMemberInfo({});
		//$(this).trigger("onload");
	}
	, $getLayer: function(){
		return this._HTMLDom["$layer"];
	}
	, setMemberInfo: function(thisData){
		this.memberInfo = thisData;
		this.display();
	}
	, getInfoData: function(){
		return this.memberInfo;
	}
	, display: function(){
		if(!this._HTMLDom) return;
		var $layer = this._HTMLDom["$layer"];
		
		var af = _util.getSelfFileName();
		var idx = null;
		
		$layer.find("nav li").removeClass("gnb_on").find("img").each(function(){
			var imgName = $(this).attr("src").replace("_on.png", ".png");
			$(this).attr("src", imgName);
		});
		
		switch(af[af.length-2]){
			case "my_home":
				idx = 0;
				break;
			case "my_score":
			case "my_score_write":
			case "my_score_rd":
				idx=1;
				break;
			case "my_alim":
			case "my_note":
			case "my_note_send":
			case "my_note_keep":
				idx = 2;
				break;
			case "my_joinroom":
				idx = 3;
				break;
			case "my_booking":
			case "my_booking_reallist":
			case "my_booking_waitinglist":
			case "my_booking_eventlist":
				idx = 4;
				break;
			case "my_mileage":
				idx = 5;
				break;
			case "my_qna_list":
			case "my_qna_write":
			case "my_qna_view":
				idx = 6;
				break;
			case "my_privacy":
			case "my_info_pw":
				idx = 7;
				break;
			case "my_out":
				idx = 8;
				break;
		}
		
		if(idx != null){var imgName = $layer.find("nav li:eq(" + idx + ") img").attr("src").replace(".png", "_on.png");
			$layer.find("nav li:eq(" + idx + ")").addClass("gnb_on").find("img").attr("src", imgName);
		}
		
		var genderStyle = {
			"M": "sex_m"
			, "F": "sex_w"
		}
		
		this.setProfileImage(false);
		$layer.find("dl.my_profile>dt:first").html(_util.nvl(this.memberInfo["닉네임"]));
		$layer.find("dl.my_profile li.sex_w").attr("class", genderStyle[this.memberInfo["성별코드"]]).html(this.memberInfo["성별"]);
		$layer.find("dl.my_profile li:eq(1)").html(this.memberInfo["지역명"]);
		$layer.find("dl.my_profile li:eq(2)").html(_util.nvl(this.memberInfo["나이대"]));
		
		var $tmp = $layer.find("dl.my_profile li.job").children().clone(true);
		$layer.find("dl.my_profile li.job").html(_util.nvl(this.memberInfo["직업"])).append($tmp);
		$layer.find("dl.my_profile li.last>span").html(_util.nvl(this.memberInfo["핸디"], "0"));
		$layer.find("dl.my_profile li:eq(5)>span").html(_util.nvl(this.memberInfo["평균타수"], "0"));
	}
	, setProfileImage: function(img){
		var $layer = this.$getLayer();
		var thisClass = this;
		
		if(img !== false) this.memberInfo["이미지"] = img;
		$layer.find("p.my_photo:first img").attr("src", _gf.filePath2Url(this.memberInfo["이미지"])).unbind("error").error(function(){
			var img = _gf.getDefaultProfileImage(thisClass.memberInfo["성별코드"]);
			$(this).attr("src", img);
		});
	}
	, $getBtnProfile: function(){
		return this._HTMLDom["$layer"].find("a").has("img[alt='프로필관리']");
	}
}

function clsMypageControl()
{
	this.objMypageTop = null;
}

clsMypageControl.prototype = {
	_init: function(){
		var thisClass = this;
		
		this.objMypageTop = new clsMypageTop();
		
		this._bind();
		
		document.objUtil.executeThread(function(){
			return _gf.getMyInfoData();
		}, function(){
			thisClass.objMypageTop.setMemberInfo(_gf.getMyInfoData());
		});
	}
	, _bind: function(){
		var thisClass = this;
			
		// 프로필 관리
		this.objMypageTop.$getBtnProfile().click(function(){
			//var winProfile = window.open("../popup/pop_profile.html", "winProfile", "width=400, height=400, scrollbars=0");
			//if(winProfile) winProfile.focus();
			
			document.location.href = _ROOT + "/mypage/my_profile.html";
		});
		
		// 개인정보변경
		
		this.objMypageTop.$getLayer().on("click", "img[alt='개인정보변경']:eq(1)", function(){
			var a = thisClass.objMypageTop.$getLayer().find("img[alt='개인정보변경']:first").parent("a").attr("href");
			
			document.location.href = a;
		});
	}
	, setProfileImage: function(img){
		this.objMypageTop.setProfileImage(img);
	}
}

document.objMypageControl = new clsMypageControl();
document.objMypageControl._init();